package com.pingsky.apm.agent.core.service.metadata;

import com.pingsky.apm.agent.api.AgentVersion;
import com.pingsky.apm.agent.api.service.BootService;
import com.pingsky.apm.agent.core.AgentId;
import com.pingsky.apm.agent.core.service.ServiceManager;
import com.pingsky.apm.agent.core.service.sender.DataSender;
import com.pingsky.apm.agent.core.util.NetworkUtil;
import com.pingsky.apm.agent.core.util.RuntimeMXBeanUtil;

public class AgentInfoService implements BootService {

    private AgentInfo agentInfo = null;

    @Override
    public void prepare() throws Throwable {

    }

    @Override
    public void start() throws Throwable {
        final String hostName = NetworkUtil.getHostName();
        final String hostIp = NetworkUtil.getRepresentationHostIp();
        final int pid = RuntimeMXBeanUtil.getPid();
        final String jvmVersion = System.getProperty("java.specification.version");

        final String agentId = AgentId.INSTANCE.getAgentId();
        final String applicationName = AgentId.INSTANCE.getApplicationName();

        this.agentInfo = new AgentInfo(agentId, applicationName, RuntimeMXBeanUtil.getVmStartTime(),
                pid, hostIp, hostName, jvmVersion, AgentVersion.VERSION);

        DataSender dataSender = ServiceManager.INSTANCE.findService(DataSender.class);
        dataSender.send(agentInfo);
    }
}
